<?php
class DBMConversor{
	public static function objectToEntity($obj){
		// get the class name
		$className = @get_class($obj);
		
		// get the entity
		$entity = DBMEntityHolder::getInstance()->getEntity($className);
		
		// check the entity integrity
		if(is_null($entity)){
			throw new IllegalEntityException($className);
		}
		
		// get the entity attributes
		$attributes = $entity->getAttributes();
		
		// search the attributes
		foreach($attributes as $attribute){
			// get the methods
			$getMethod = DBMReflectionHelper::findGetter($className, $attribute->getName());
			
			// get the value of the attribute
			$value = $obj->$getMethod();
			
			// set the value
			$attribute->setValue($value);
		}
		
		// return the entity 
		return $entity;
	}
	
	public static function resultsetToObject(DBMResultSet $resultset, DBMEntity $entity){
		// create the collection and the field specification
		$collection = array();
		$fields		= array();
		
		// get the entity class
		$entityClass 	= $entity->getClass();
		$relationships	= $entity->getRelationships();
		
		//die(var_dump($entity));
		
		// search the fields and find possible relationships on the fields
		foreach($resultset->getFields() as $field){
			// check if the main entity owns this field
			$attributeData = $entity->getAttributeByString($field);
			
			// check if the attribute have been found
			if(is_null($attributeData)){
				// search the relationships
				foreach($relationships as $relationship){
					// get the target entity
					$targetEntity = DBMEntityHolder::getInstance()->getEntity($relationship->getTargetEntity());
					
					// check if the entity owns this field
					$attributeData = $targetEntity->getAttributeByString($field);
					
					// check if the attribute have been found 
					if(is_null($attributeData)){
						continue;
					} else {
						// check if the target object has been instantiated
						$relationshipGetter = DBMReflectionHelper::findGetter($entity->getClass(), $relationship->getAttribute());
						$relationshipSetter = DBMReflectionHelper::findSetter($entity->getClass(), $relationship->getAttribute());
						
						// get the target class
						$attributeParent 	= $targetEntity->getClass();
						$attributeName		= $attributeData->getName();
						
						// set the attribute data
						$setter = DBMReflectionHelper::findSetter($attributeParent, $attributeName);
						$getter = DBMReflectionHelper::findSetter($attributeParent, $attributeName);
						
						// store the field data
						$fields[$field] = array(
							"getter" => $getter, 
							"setter" => $setter, 
							"class" => $attributeParent, 
							"relationship" => array(
								"getter" => $relationshipGetter, 
								"setter" => $relationshipSetter
							)
						);
					}
				}
			} else {
				// get the name of the attribute
				$attributeName = $attributeData->getName();
				
				// get the name of the setter method
				$setter = DBMReflectionHelper::findSetter($entityClass, $attributeName);
				$getter = DBMReflectionHelper::findGetter($entityClass, $attributeName);

				// store the field data
				$fields[$field] = array(
					"getter" => $getter, 
					"setter" => $setter, 
					"class" => null, 
					"relationship" => null
				);
			}
		}
		
		// search the resultset
		foreach($resultset as $result){
			// create the instance of the object
			$object = new $entityClass();
			
			// get the data on the result
			$values = $result->getAll();
			
			// search the data
			foreach($values as $field => $value){
				if(!isset($fields[$field])){
					continue;
				}
				
				// get the field data
				$fieldData = $fields[$field];
				
				// check if this is a primary attribute or a relationship attribute
				if(is_null($fieldData["class"])){
					// get the setter
					$setter = $fieldData["setter"];
					
					// set the data
					$object->$setter($value);
				} else {
					// get the setter
					$setter = $fieldData["setter"];
					
					// get the relationship getter and setter
					$relationshipGetter = $fieldData["relationship"]["getter"];
					$relationshipSetter = $fieldData["relationship"]["setter"];
					
					// check if the relationship class must be created 
					if(!is_object($object->$relationshipGetter())){
						// create the relationship object
						$relationshipObject = new $fieldData["class"]();
						
						// set the relationship object on the main entity
						$object->$relationshipSetter($relationshipObject);
					} else {
						// get the relationship object
						$relationshipObject = $object->$relationshipGetter();
					}
					
					// set the data
					$relationshipObject->$setter($value);
				}
			}
			
			// store
			$collection[] = $object;
		}
		
		// return 
		return $collection;
	}

	public static function commandToQuery(DBMCommand $command, DBMEntityManager $entityMgr){
		// convert
		return $entityMgr->getConnection()->toSQL($command);
	}
} 
?>